﻿
$verify = $eswat_test.Services.Verify

# ---------------------------------------------------------------
#  TEST EXTERNAL .NET COMPONENT (.dll)
# ---------------------------------------------------------------

# require/include the component
require 'Eswat.Samples.SampleComponentLibrary.dll'
include Eswat::Samples::SampleComponentLibrary

# create new component
$comp = SampleComponent.new
#set and verify the title
$comp.Title = "hello component world!"
$verify.AreEqual("hello component world!", $comp.Title, "sample component title should match")

# ---------------------------------------------------------------
#  TEST WEBSITE
# ---------------------------------------------------------------

# alias ESWAT web test service for shorter reference
$web = $eswat_test.Services.Web

# open IE and go to project "silk"
$web.open("IE")
$web.go("http://localhost:61614/Auth/SignOut") # always start at sign-out page to ensure we don't resume a previously logged-in session

# get current bg color
$originalBG = $web.js('return document.body.style.backgroundColor;')
# set new bg color and return it
$newBG = $web.js('return (document.body.style.backgroundColor = "#FF0000");')
# verify new bg color was set
$verify.AreEqual("#FF0000", $newBG, "bg color")
# restore original bg color
$web.js("document.body.style.backgroundColor = \"#{$originalBG}\"")

# verify page title
$verify.AreEqual("Mileage Stats | Know where your gas takes you", $web.pageTitle, "Silk landing page title should match")
# verify html.head.title
$verify.AreEqual("Mileage Stats | Know where your gas takes you", $web.title.text, "Silk landing title should match")

# verify <h1> text using Selenium's "By" syntax
include OpenQA::Selenium
$verify.AreEqual("Sign In / Register", $web.find(By.TagName("h1")).text, "h1 text")

# test images shortcut
$verify.AreEqual(6, $web.images.Count, "silk images")
# test byId shortcut
$verify.IsNotNull($web.byId("pinnedSiteContainer"), "silk by id")
# test oneById shortcut
$verify.IsNotNull($web.oneById("pinnedSiteContainer"), "silk one by id")
# test allByTag shortcut
$verify.AreEqual(6, $web.allByTag("img").Count, "silk images all by tag")
# test allByClass shortcut
$verify.AreEqual(2, $web.allByClass("button").Count, "silk all by class name")
# test oneByName shortcut
$verify.IsNotNull($web.oneByName("providerUrl"), "silk by name")
# test attr shortcut
$providerUrl = $web.oneByName("providerUrl")
$originalProvider = $providerUrl.attr("value")
$verify.AreEqual("http://oturner.myidprovider.org/", $originalProvider, "silk attribute")
# test type shortcut
$providerUrl.type("ironruby.net")
$verify.AreEqual("ironruby.net", $providerUrl.attr("value"), "silk type text")
$providerUrl.type($originalProvider)
# test submit shortcut
$providerUrl.submit()

# test navigation result (next page)
$verify.AreEqual("Mock Authentication", $web.pageTitle, "Silk login title should match")
$web.byId("claimed_identifier").submit()

# test another navigation result (next page)
$verify.AreEqual("Dashboard", $web.pageTitle, "Silk login title should match")
$verify.AreEqual("Sample User", $web.byId("welcome").oneByTag("span").text, "welcome name");
$nav = $web.oneByClass("nav").allByTag("a")
# test properties of all...() shortcut result
$verify.AreEqual(4, $nav.Count, "nav count")
$verify.AreEqual("Dashboard", $nav[0].text, "nav dashboard")
$verify.AreEqual("Charts", $nav[1].text, "nav charts")
$verify.AreEqual("Profile", $nav[2].text, "nav profile")
$verify.AreEqual("Sign Out", $nav[3].text, "nav sign out")
# test oneByPartialLinkText shortcut (actual link text is "+ Add Vehicle"
$verify.IsNotNull($web.oneByPartialLinkText("Add Vehicle"), "oneByPartialLinkText")
# test oneByLinkText shortcut
$verify.IsNotNull($web.oneByLinkText("+ Add Vehicle"), "oneByLinkText")

$web.go("http://localhost:61614/Auth/SignOut")

# quit (close IE)
$web.quit()

# ---------------------------------------------------------------
#  TEST "verify" API
# ---------------------------------------------------------------

$verify.True(TRUE)
$verify.True(TRUE, "True(TRUE) should succeed")
$verify.True(TRUE, "True({0}) should succeed", "True")
$verify.True(FALSE, "True(FALSE) should fail")

$verify.IsTrue(TRUE)
$verify.IsTrue(TRUE, "IsTrue(TRUE) should succeed")
$verify.IsTrue(TRUE, "IsTrue({0}) should succeed", "True")
$verify.IsTrue(FALSE, "IsTrue(FALSE) should fail")

$verify.False(FALSE)
$verify.False(FALSE, "False(FALSE) should succeed")
$verify.False(FALSE, "False({0}) should succeed", "FALSE")
$verify.False(TRUE, "False(TRUE) should fail")

$verify.IsFalse(FALSE)
$verify.IsFalse(FALSE, "IsFalse(FALSE) should succeed")
$verify.IsFalse(FALSE, "IsFalse({0}) should succeed", "FALSE")
$verify.IsFalse(TRUE, "IsFalse(TRUE) should fail")

# bool
$verify.AreEqual(TRUE, TRUE)
$verify.AreEqual(TRUE, TRUE, "AreEqual(TRUE, TRUE) should succeed")
$verify.AreEqual(TRUE, TRUE, "AreEqual({0}) should succeed", "TRUE, TRUE")
$verify.AreEqual(TRUE, FALSE, "AreEqual(TRUE, FALSE) should fail")

# float -> passed to .NET as Double
$verify.AreEqual(0.0, 0.0)
$verify.AreEqual(0.0, 0.0, "AreEqual(0.0, 0.0) should succeed")
$verify.AreEqual(0.0, 0.0, "AreEqual({0}) should succeed", "0.0, 0.0")
$verify.AreEqual(0.0, 0.1, "AreEqual(0.0, 0.1) should fail")

nil # script return